package edu.pku.sei.modeler.figures;

import static java.lang.Math.max;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Rectangle;

public class ExecutionOccurrenceFigure extends CommonFigure {
	
	private boolean canMove = true;
	
	public ExecutionOccurrenceFigure() {
		this.setOpaque(true);
	}
	
	
    public void setCanMove(boolean canMove) {
    	this.canMove = canMove;
    }
    
    @Override
    public void setBounds(Rectangle rect) {
    	if (this.canMove || this.getBounds().width <= 0 || this.bounds.height <= 0) {
    		super.setBounds(rect);
    	}
    	else {
    		rect.x = this.getBounds().x;
    		super.setBounds(rect);
    	}
    }
    
	private Rectangle SIZE = new Rectangle(0, 0, 11, 1);

	protected void paintFigure(Graphics g) {
		Rectangle r = getBounds().getResized(-1, -1);
		g.setForegroundColor(ColorConstants.black);
		g.setBackgroundColor(this.getBackgroundColor());
		g.fillRectangle(r);
		g.drawRectangle(r);
		this.setOpaque(true);
	}

	/*
	 * @Override public Rectangle getPreferredBounds(Rectangle suggestedBounds) {
	 * return new Rectangle( suggestedBounds.x - SIZE.width / 2,
	 * suggestedBounds.y, SIZE.width, Math.); }
	 */

	// @Override - InteractionFigure1
	public Rectangle getPreferredModelBounds(final Rectangle bounds, final Rectangle coveredBounds) {
		if (coveredBounds != null) {
			return new Rectangle(coveredBounds.x + coveredBounds.width / 2, bounds.y, 1, max(bounds.height, 5));
		} else
			return bounds.getCopy();
	}

	// @Override - InteractionFigure1
	public Rectangle getPreferredFigureBounds(final Rectangle bounds, final Rectangle coveredBounds) {
		Rectangle preferredLocation = getPreferredModelBounds(bounds, coveredBounds);
		return new Rectangle(preferredLocation.x - SIZE.width / 2, preferredLocation.y, SIZE.width, preferredLocation.height);
	}

	
	// --- ILabeledFigure ---

	// Labels on EOs are currently not supported (but it is allowed in the
	// UML specification)

	public Rectangle getLabelBounds() {
		return getBounds().getCopy();
	}

	public void setLabel(String labelText) {
		// ignore
	}
}
